package cn.hfatec.shms.system.register.service;

import java.util.Date;
import java.util.List;
import java.util.HashMap;


import cn.hfatec.shms.system.user.model.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.system.register.model.Register;
import cn.hfatec.shms.system.register.dao.RegisterDao;
import cn.hfatec.shms.system.register.model.RegisterParams;

import javax.annotation.Resource;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("RegisterService")
public class RegisterService {

    public static final String BEAN_ID = "RegisterService";

    //注入Dao层
    @Resource(name = "RegisterDao")
    private RegisterDao registerDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param registerParams
     * @param page
     *
     */
    @Transactional
    public Table queryRegisterTable(RegisterParams registerParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", registerParams);
        return baseDao.queryTableByPage("queryRegister", params, page);
    }

    /**
     *
     * 插入数据
     * @param register
     *
     */
    @Transactional
    public void saveRegister(Register register, User user) {
        if(register.getId() != null) {
            Register result = getRegister(register.getId());
            if(result != null) {
                registerDao.update(register);
            } else {
                register.setCreateDate(new Date());
                register.setUserName(user.getUserName());
                registerDao.insert(register);
            }
        } else {
            register.setCreateDate(new Date());
            register.setUserName(user.getUserName());
            registerDao.insert(register);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteRegister(Integer id) {

        registerDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param registerList
     *
     */
    @Transactional
    public void deleteRegisterBatch(List<String> registerList) {
        for(String id : registerList) {
            deleteRegister(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public Register getRegister(Integer id) {

        return registerDao.getRegisterById(id);
    }
}